﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using SurfaceCardsBoardLibrary.Interfaces;
using WPFMitsuControlsv2;

namespace SurfaceCardsGameKit.Games.BasicGame
{
    /// <summary>
    /// Interaction logic for HelpBook.xaml
    /// </summary>
    public partial class HelpBook : ScatterViewItem, IManipulableObject
    {
        public List<HelpBookPage> Pages { get; set; }

        public HelpBook()
        {
            Pages = new List<HelpBookPage>()
            {
                new HelpBookPageLeft("Turn", "return card.wmv"),
                new HelpBookPageRight("Card turn", new HelpBookActionResult("Double tap the card", "Turns the card")),

                new HelpBookPageLeft("Flip", "flip.wmv"),
                new HelpBookPageRight("Card flip", new HelpBookActionResult("Put two fingers on the card, and then a third tapping on it.\r\nAny finger up hides the card again", "Flips the corner")),

                new HelpBookPageLeft("Fan", "eventail.wmv"),
                new HelpBookPageRight("Fan disposition", new HelpBookActionResult("Put a finger on each grip then move them away from each other", "Changes cards disposition to fan")),

                new HelpBookPageLeft("Return finger", "return all cards.wmv"),
                new HelpBookPageRight("Turn over all cards", new HelpBookActionResult("Select \"return cards\" in the deck menu. Put a finger on one grip and slide an other finger on all cards", "Turns the cards while the finger slides over them")),

                new HelpBookPageLeft("Eye", "eye.wmv"),
                new HelpBookPageRight("Eye transparency", new HelpBookActionResult("Put a finger on on the eye icon and move it on the cards keeping the finger pressed", "Applies a transparency effect on all cards to enable cards preview")),

                new HelpBookPageLeft("Mosaic", "mosaic.wmv"),
                new HelpBookPageRight("Mosaic view", new HelpBookActionResult("Tap on the mosaic icon", "Enables or disables mosaic view")),

                new HelpBookPageLeft("Sel.", "selection.wmv"),
                new HelpBookPageRight()
                {
                    Title = "Cards selection", 
                    ActionResults = new List<HelpBookActionResult>() {
                        new HelpBookActionResult("Tap on a card", "Selects the card by offseting it"),
                        new HelpBookActionResult("Drag a selected card out of the deck", "Creates a deck with all previously selected cards"),
                    }
                },

                new HelpBookPageLeft("Cut", "cut.wmv"),
                new HelpBookPageRight("Cut a deck", new HelpBookActionResult("Select \"cut\" in the deck menu then choose a card in the deck.", "Cuts the deck at the selected card.")),
                
                new HelpBookPageLeft("Shuffle", "shuffle.wmv"),
                new HelpBookPageRight("Shuffle a deck", new HelpBookActionResult("Shake the deck", "Shuffles cards' position")),

                new HelpBookPageLeft("Heap/Stack", "pile tas.wmv"),
                new HelpBookPageRight("Deck disposition", new HelpBookActionResult("Select heap or stack disposition in the deck menu", "Changes cards disposition in the deck")),

                new HelpBookPageLeft("Adding Pos.", "cards adding position.wmv"),
                new HelpBookPageRight("Cards Adding Pos.", new HelpBookActionResult("Select top or bottom in the deck menu", "A card added to the deck will be on top or bottom")),

                new HelpBookPageLeft("Adding Face", "cards adding face.wmv"),
                new HelpBookPageRight("Cards Adding Face", new HelpBookActionResult("Select front, back or \"ignore\" in the deck menu", "A card added to the deck will be turned if necessary")),

                new HelpBookPageLeft("Return all", "return all cards auto.wmv"),
                new HelpBookPageRight("Return all cards", new HelpBookActionResult("Selection \"all front\" or \"all back\" icon in the deck menu", "All cards in the deck will be turned according to the selection")),

                new HelpBookPageLeft("Split", "split.wmv"),
                new HelpBookPageRight("Split deck", new HelpBookActionResult("Select the slit icon in the deck menu then select a card in the deck.", "Splits the deck in to other decks.")),

            };

            InitializeComponent();
        }

        #region IManipulableObject Members

        public new Point ActualCenter
        {
            get;
            set;
        }

        public Point LastCenterBeforeContactsUp
        {
            get;
            set;
        }

        public ScatterView ScatterView
        {
            get;
            set;
        }

        public new ScatterViewItem ScatterViewItem
        {
            get;
            set;
        }

        public SurfaceCardsBoardLibrary.Board Board
        {
            get;
            set;
        }

        public double DefaultWidth
        {
            get;
            set;
        }

        public double DefaultHeight
        {
            get;
            set;
        }

        #endregion

        private void book_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // get the video for current page

            var pageLeft = GetSelectedPageLeft();
            var bi = GetSelectedLeftBookItem();

            if (pageLeft != null && pageLeft.MediaElement != null)
            {
                pageLeft.MediaElement.Play();
                pageLeft.MediaElement.MediaEnded += new RoutedEventHandler(MediaElement_MediaEnded);
            }

            // stop playing for all others
            foreach (MediaElement me in Pages.OfType<HelpBookPageLeft>().Where(p => p.MediaElement != null && p != pageLeft).Select(p => p.MediaElement))
            {
                me.MediaEnded -= new RoutedEventHandler(MediaElement_MediaEnded);
                me.Stop();
            }

        }

        void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            var me = (MediaElement)sender;

            me.Position = TimeSpan.FromMilliseconds(0);
            me.Play();
        }

        private HelpBookPageLeft GetSelectedPageLeft()
        {
            var leftIndex = book.SelectedIndex % 2 == 0 ?
                book.SelectedIndex - 1 : book.SelectedIndex;

            if (leftIndex != -1)
                return book.Items[leftIndex] as HelpBookPageLeft;

            return null;
        }

        private BookItem GetSelectedLeftBookItem()
        {
            var index = book.SelectedIndex % 2 == 0 ?
                book.SelectedIndex - 1 : book.SelectedIndex;

            if (index >= book.Items.Count || index < 0)
                return null;

            BookItem item = book.Items[index] as BookItem;

            if (item == null)
                item = book.ItemContainerGenerator.ContainerFromIndex(index) as BookItem;

            return item;
        }

        public ICommand CloseBookCommand { get; set; }

    }

    public abstract class HelpBookPage
    {

    }

    public class HelpBookPageLeft : HelpBookPage
    {
        public HelpBookPageLeft(string bookmarkTitle, string videoSource)
        {
            VideoSource = new Uri(@"Games\BasicGame\MoviesBook\" + videoSource, UriKind.Relative);
            MediaElement = new MediaElement()
            {
                LoadedBehavior = MediaState.Manual,
                UnloadedBehavior = MediaState.Manual,
                Stretch = Stretch.Fill,
                Source = VideoSource
            };
            BookmarkTitle = bookmarkTitle;
        }

        public string BookmarkTitle { get; set; }
        public Uri VideoSource { get; set; }
        public MediaElement MediaElement { get; set; }
    }

    public class HelpBookPageRight : HelpBookPage
    {
        public HelpBookPageRight()
        {

        }

        public HelpBookPageRight(string title, HelpBookActionResult actionResult)
        {
            Title = title;
            ActionResults = new List<HelpBookActionResult>() { actionResult };
        }

        public string Title { get; set; }
        public List<HelpBookActionResult> ActionResults { get; set; }
    }

    public class HelpBookActionResult
    {
        public HelpBookActionResult(string action, string result)
        {
            ActionLines = action;
            ResultLines = result;
        }

        public string ActionLines { get; set; }
        public string ResultLines { get; set; }
    }

    public class HelpbookDataTemplateSelector : DataTemplateSelector
    {

        public DataTemplate FreeTemplate { get; set; }
        public DataTemplate PageLeftTemplate { get; set; }
        public DataTemplate PageRightTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is HelpBookPageLeft)
                return PageLeftTemplate;
            else if (item is HelpBookPageRight)
                return PageRightTemplate;
            else
                return FreeTemplate;
        }
    }
}
